<?php

include_once "IModel.php";
include_once "ImageEditor.php";
include_once "StyleEditor.php";
include_once "User.php";
include_once "UserDAO.php";
include_once "UserPage.php";
include_once "UserPageDAO.php";

class SettingsModel implements IModel {
	
	public function getData($params) {
		$data->pageOwner=$params->pageOwner;
		if(Session::getLoggedUserName()!=$params->pageOwner) {
			$data->userPage=null;
			$data->isYours=false;
			return $data;
		}
		$data->isYours=true;		
		if($params->action == 'changeSettings') {			
			$data->userPage=null;
			if($params->postParams->password == null)
				return $data;
			$user=UserDAO::getInstance()->getUser($data->pageOwner);
			$oldPage=UserPageDAO::getInstance()->getUserPage($user->getId());
			if($user->getPassHash()!=Hasher::hash($params->postParams->password))
				return $data;
			$userPage=new UserPage();
			if($params->postParams->newpassword != null) {
				// zmiana hasła
				$user=UserDAO::getInstance()->getUser($data->pageOwner);
				UserDAO::getInstance()->changePassword($user->getId(), $params->postParams->newpassword);
			}
			if($params->postParams->title != null)
				$userPage->setPageTitle($params->postParams->title);
			else
				$userPage->setPageTitle($oldPage->getPageTitle());
			if($params->postParams->subtitle != null)
				$userPage->setSubtitle($params->postParams->subtitle);
			else
				$userPage->setSubTitle($oldPage->getPageSubtitle());
			if($params->postParams->header != null)
				$userPage->setCustomHeader($params->postParams->header);
			else
				$userPage->setCustomHeader($oldPage->getCustomHeader());
			if($params->postParams->about != null)
				$userPage->setAbout($params->postParams->about);
			else
				$userPage->setAbout($oldPage->getAbout());
			if($params->files->css['name']!=null) {
				$typecss = StyleEditor::getType(basename($params->files->css['name']));
				if($typecss == StyleEditor::TYPE_UNKNOWN)
				{
					$data->wrongcss=1;
					return $data;
				}
				$targetPath = URI::getCssUploadDir();
				$targetPath .= '/'.$params->pageOwner.'.css';
				move_uploaded_file($params->files->css['tmp_name'], $targetPath);
				$userPage->setCssUrl($targetPath);
			}
			else
				$userPage->setCssUrl($oldPage->getCssUrl());			
			
			if($params->files->avatar['name']!=null) {
				$targetPath = URI::getAvatarsUploadDir();
				$type = ImageEditor::getType(basename($params->files->avatar['name']));
				if($type == ImageEditor::TYPE_UNKNOWN)
				{
					$data->wrongavatar=1;
					return $data;
				}
				$targetPath .= '/'.$params->pageOwner.'.'.ImageEditor::getExtension($type);
				list($width, $height) = ImageEditor::getSize($params->files->avatar['tmp_name']);
				if($width > $height)
					$image = ImageEditor::resizeToWidth($params->files->avatar['tmp_name'], Config::AVATAR_WIDTH, $type);
				else
					$image = ImageEditor::resizeToHeight($params->files->avatar['tmp_name'], Config::AVATAR_HEIGHT, $type);
				ImageEditor::saveImage($image, $type, $targetPath);
				//zmień avatar
				$user=UserDAO::getInstance()->getUser($data->pageOwner);	
				echo $targetPath;	
				UserDAO::getInstance()->changeAvatar($user->getId(), $targetPath);
			}
			$data->headerEntries[] = '<meta HTTP-EQUIV="REFRESH" content="2; url='.URI::getUrl(null, $data->pageOwner).'">';
			$data->userPage=$userPage;
			echo "In setting model! <br />";
			echo $userPage->getString();
			
			UserPageDAO::getInstance()->changeUserPage($user->getId(), $userPage);
		}
		return $data;
	}
}

?>